package com.breezelin.freshmarket.network.apis;

/**
 * Created by Breeze Lin
 * 2016/9/23 14:06
 * breezesummerlin@163.com
 */


import com.breezelin.freshmarket.entities.EmptyEntity;
import com.breezelin.freshmarket.entities.JsonStruct;
import com.breezelin.freshmarket.entities.data.UserInfo;
import com.breezelin.freshmarket.entities.network.ReqHeader;
import com.breezelin.freshmarket.entities.network.ReqNRsp;
import com.breezelin.freshmarket.entities.req.LoginInfo;
import com.breezelin.freshmarket.entities.req.RegisterReq;
import com.breezelin.freshmarket.entities.req.VerifyCodeReq;
import com.breezelin.freshmarket.network.ApiResolver;

import org.json.JSONObject;

/**
 * 账户相关api
 */
public final class ProfileApi {
	private ProfileApi() {
		// 封闭构造
	}

	/**
	 * 获取验证码
	 */
	public static final String REQUEST_VERIFY_CODE = "yiguo.mapi.user.smscode";
	/**
	 * 注册
	 */
	public static final String REGISTER = "yiguo.mapi.user.register";
	/**
	 * 登录
	 */
	public static final String LOGIN = "yiguo.mapi.user.login";
	/**
	 * 获取用户信息
	 */
	public static final String GET_USER_INFO = "yiguo.mapi.user.get";

	/**
	 * 请求发送验证码
	 *
	 * @param phoneNo 电话号码
	 */
	public static ReqNRsp<JsonStruct> requestVerifyCode(String phoneNo) {
		ReqHeader reqHeader = new ReqHeader(0, 0, REQUEST_VERIFY_CODE);
		VerifyCodeReq codeBody = new VerifyCodeReq(phoneNo);
		ReqNRsp<VerifyCodeReq> req = new ReqNRsp<>(reqHeader, codeBody);
		JSONObject jsonObject = ApiResolver.defaultRequest(req);
		ReqNRsp<JsonStruct> rsp = new ReqNRsp<>(null, null);
		rsp.fromJson(jsonObject);
		return rsp;
	}

	/**
	 * 注册请求
	 *
	 * @param phone      电话号码
	 * @param verifyCode 验证码
	 * @param password   密码
	 * @return 用户信息
	 */
	public static ReqNRsp<UserInfo> register(String phone, String verifyCode, String password) {
		ReqHeader reqHeader = new ReqHeader(0, 0, REGISTER);
		RegisterReq reqBody = new RegisterReq(phone, phone, password, verifyCode);
		ReqNRsp<RegisterReq> req = new ReqNRsp<>(reqHeader, reqBody);
		JSONObject jsonObject = ApiResolver.defaultRequest(req);
		ReqNRsp<UserInfo> rsp = new ReqNRsp<>(null, new UserInfo());
		rsp.fromJson(jsonObject);
		return rsp;
	}

	/**
	 * 登录请求
	 *
	 * @param userName 用户名
	 * @param password 密码
	 * @return 用户信息
	 */
	public static ReqNRsp<UserInfo> login(String userName, String password) {
		ReqHeader reqHeader = new ReqHeader(0, 0, LOGIN);
		LoginInfo reqBody = new LoginInfo(userName, password);
		ReqNRsp<LoginInfo> req = new ReqNRsp<>(reqHeader, reqBody);
		JSONObject jsonObject = ApiResolver.defaultRequest(req);
		ReqNRsp<UserInfo> rsp = new ReqNRsp<>(null, new UserInfo());
		rsp.fromJson(jsonObject);
		return rsp;
	}

	/**
	 * 获取用户信息
	 *
	 * @return 用户信息
	 */
	public static ReqNRsp<UserInfo> getUserInfo() {
		ReqHeader reqHeader = new ReqHeader(0, 0, GET_USER_INFO);
		JsonStruct reqBody = new EmptyEntity();
		ReqNRsp<JsonStruct> req = new ReqNRsp<>(reqHeader, reqBody);
		JSONObject jsonObject = ApiResolver.defaultRequest(req);
		ReqNRsp<UserInfo> rsp = new ReqNRsp<>(null, new UserInfo());
		rsp.fromJson(jsonObject);
		return rsp;
	}
}
